package com.mybatis.demo.plugin;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zp
 * @version 1.0
 * @description: sql监控的上下文对象
 * private Map<String,Integer> selectMap; //查询语句和次数
 * private Map<String,Integer> updateMap; //修改语句和次数
 * private Map<String,Integer> deleteMap; //删除语句和次数
 * private Map<String,Integer> insertMap; //新增语句和次数
 * private Map<String,Integer> countMap; //增删改查语句总次数
 * private static SqlContext sqlContext; //单例对象
 * @date 2022/11/26 10:36
 */

public class SqlContext {
    private Map<String,Integer> selectMap;
    private Map<String,Integer> updateMap;
    private Map<String,Integer> deleteMap;
    private Map<String,Integer> insertMap;
    private Map<String,Integer> countMap;
    private static SqlContext sqlContext;
    public final static String SELECT = "select";
    public final static String UPDATE = "update";
    public final static String DELETE = "delete";
    public final static String INSERT = "insert";
    /**
     * @description:  私有化对象 让全局只有一个sql 监控的上下文对象
     * @author zp
     * @date: 2022/11/26 10:37
     */
    private SqlContext(){

    }

    public static SqlContext getSqlContext(){
        if(sqlContext == null){
            // 初始化参数
            sqlContext = new SqlContext();
            sqlContext.setDeleteMap(new HashMap<>(10));
            sqlContext.setSelectMap(new HashMap<>(10));
            sqlContext.setInsertMap(new HashMap<>(10));
            sqlContext.setUpdateMap(new HashMap<>(10));
            Map<String,Integer> map = new HashMap<>(4);
            map.put(SqlContext.SELECT,0);
            map.put(SqlContext.UPDATE,0);
            map.put(SqlContext.INSERT,0);
            map.put(SqlContext.DELETE,0);
            sqlContext.setCountMap(map);
        }
        return sqlContext;
    }

    public Map<String, Integer> getSelectMap() {
        return selectMap;
    }

    private void setSelectMap(Map<String, Integer> selectMap) {
        this.selectMap = selectMap;
    }

    public Map<String, Integer> getUpdateMap() {
        return updateMap;
    }

    private void setUpdateMap(Map<String, Integer> updateMap) {
        this.updateMap = updateMap;
    }

    public Map<String, Integer> getDeleteMap() {
        return deleteMap;
    }

    private void setDeleteMap(Map<String, Integer> deleteMap) {
        this.deleteMap = deleteMap;
    }

    public Map<String, Integer> getInsertMap() {
        return insertMap;
    }

    private void setInsertMap(Map<String, Integer> insertMap) {
        this.insertMap = insertMap;
    }

    public Map<String, Integer> getCountMap() {
        countMap.put(SqlContext.SELECT,selectMap.size());
        countMap.put(SqlContext.UPDATE,updateMap.size());
        countMap.put(SqlContext.INSERT,insertMap.size());
        countMap.put(SqlContext.DELETE,deleteMap.size());
        return countMap;
    }

    private void setCountMap(Map<String, Integer> countMap) {
        this.countMap = countMap;
    }
}
